package com.example.qa_back.controller;

import com.example.qa_back.entity.QA;
import com.example.qa_back.entity.Response;
import com.example.qa_back.service.FrontService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 董帅
 * @date 2021/3/13 - 11:06
 */
@RestController
@RequestMapping("/api/front")
public class FrontController {
    private final FrontService frontService;

    public FrontController(FrontService frontService) {
        this.frontService = frontService;
    }

    /*
    * 返回答案
    * */
    @PostMapping("/getAnswer")
    public Response getAnswer(@RequestBody Map<String, String> map) {
        String question = map.get("question");
        Object returnAnswer = frontService.returnAnswer(question);
        if (returnAnswer.equals("error")) {
            return new Response(2, "python服务器出现错误", null);
        } else {
            return new Response(1, "获取成功", (QA)returnAnswer);
        }
    }

    /***
     * 也是返回答案，不过是使用了深度学习的模型
     */
    @PostMapping("/getAnswerDP")
    public Response getAnswerDP(@RequestBody Map<String, String> map) {
        String question = map.get("question");
        Object returnAnswerDP = frontService.returnAnswerDP(question);
        if (returnAnswerDP.equals("error")) {
            return new Response(2, "python服务器出现错误", null);
        } else {
            return new Response(1, "获取成功", (QA)returnAnswerDP);
        }
    }

    /***
     * 插入返回答案不正确的问题
     */
    @PostMapping("/insertCQ")
    public Response insertCQ(@RequestBody Map<String, String> map) {
        String question = map.get("question");
        frontService.insertCQ(question);
        return new Response(1, "插入成功", null);
    }
}
